package com.pinc.akb48;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoPlayer extends Activity implements
MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener {
	public static final String TAG = "VideoPlayer";
	private VideoView mVideoView;
	private Uri mUri;
	private int mPositionWhenPaused = -1;
	private MediaController mMediaController;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.video);

		mVideoView = (VideoView) findViewById(R.id.video_view);

		mUri = Uri.parse(getIntent().getStringExtra("url"));

		// Create media controller
		mMediaController = new MediaController(this);
		mVideoView.setMediaController(mMediaController);
		mVideoView.setOnCompletionListener(this);
	}

	@Override
	public void onStart() {
		super.onStart();
		// Play Video
		mVideoView.setVideoURI(mUri);
		mVideoView.start();
	}

	@Override
	public void onPause() {
		super.onPause();
		// Stop video when the activity is pause.
		mPositionWhenPaused = mVideoView.getCurrentPosition();
		mVideoView.stopPlayback();
		Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);
		Log.d(TAG, "OnStop: getDuration  = " + mVideoView.getDuration());
	}

	@Override
	public void onResume() {
		super.onResume();
		// Resume video player
		if (mPositionWhenPaused >= 0) {
			mVideoView.seekTo(mPositionWhenPaused);
			mPositionWhenPaused = -1;
		}
	}

	public boolean onError(MediaPlayer player, int arg1, int arg2) {
		return false;
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		mp.stop();
		VideoPlayer.this.finish();
	}
}
